<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<%@taglib uri="http://www.tag.com/website" prefix="s" %>
<link href="${webctx}css/scrollbar.css" rel="stylesheet" type="text/css"/>
<script src="${ctx }/plugins/textfader/js.js"></script>
<script src="${webctx}js/scrollbar.min.js"></script>
<div class="msg-cart"
     style="z-index: 100; display: none; position: fixed; top: 50%;left: 50%; padding:0 20px; height: 50px;text-align:center; line-height: 50px; font-size:18px; -webkit-border-radius: 50px;-moz-border-radius: 50px;border-radius: 50px; color: #fff;background: #666;-webkit-box-shadow: 0 0 10px 0 #ccc;-moz-box-shadow: 0 0 10px 0 #ccc;box-shadow: 0 0 10px 0 #ccc;">
    加入购物车成功
</div>
<div id="wrap-head">
    <%--<div style="width: 100%;margin: 0 auto;">--%>
    <%--<a style="display: block;width: 100%;height: 120px;background: url(https://cdn.xyw66.com/info/0000201705091112.gif) no-repeat center center;" href="https://www.xyw66.com/product/subject?channelcode=001005"></a>--%>
    <%--</div>--%>
    <div class="head inner clearfix">
        <div class="logo">
            <a href="${ctx }/website/index.jsp"><img src="${webctx }/images/logo.png"/></a>
            <div id="toptxtfader" style="width:310px;height:30px;overflow:hidden">
                <ul>
                    <!-- <li style="line-height:26px;">互联网药品交易服务资格证书：${syspro.hlwjyzg}</li>
				<li style="line-height:20px;">药品经营许可证：${syspro.ypjyxk}</li>
				 -->
                </ul>
            </div>
        </div>
        <div class="head-search" style="margin-top: 26px;">
            <div class="search-input">
                <div class="input-box">
                    <form action="" method="get" onsubmit="return false;">
                        <input class="input-md" id="topsearchtitle" name="topsearchtitle"
                               onfocus="curinutfocus='search'" onblur="curinutfocus=''" type="text"
                               style="height:40px; line-height:36px;" value="${querylike }" onkeyup=""
                               placeholder="输入商品名称，通用名，商品编号，生产厂家"/>
                    </form>
                </div>
                <button type="submit" class="search-btn" onclick="searchProduct()"></button>
            </div>
            <ul class="search-nav pt-10">
                <li>热门搜索：</li>
                <s:hotword>
                    <c:if test="${type !=1}">
                        <li id="${id}"><input type="hidden" name="shopname" value="${name}">
                            <input type="hidden" name="shopurl" value="${url}">
                            <a href="javascript:seetshop(${id});">${name }</a></li>
                    </c:if>
                </s:hotword>
            </ul>
        </div>
        <%--<div class="wechat">--%>
        <%--<img src="${fileserver }${syspro.webqrcode}" style="width:100"/>--%>
        <%--</div>--%>
        <div class="shopping-cart">
            <a href="${ctx}/order/shopcart">
                <div class="cart-main"><span><img src="${webctx}images/ico-cart.png" alt=""><i class="homecount"
                                                                                               id="cart_num">0</i></span>购物车
                </div>
            </a>
            <div class="shopping-cart-cont clearfix">
                <div class="list-title">最近添加的商品</div>
                <div id="cart_content_head">
                    <!-- 未登录状态 -->
                    <div style="display: none" class="log-out">
                        <p>购物车中还没有商品</p>
                        <p>赶快登录查看我的商品吧</p>
                        <a href="${ctx}/user/showlogin" class="log-in-btn">登录</a>
                    </div>

                    <!-- 登录没商品状态 -->
                    <div style="display: none" class="cart-null">你尚未添加商品到购物车，赶紧去逛逛吧~</div>

                    <!-- 登录有商品状态 -->

                    <div style="display: none" class="cart-cont-list" id="side">
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    $(function () {
        showCartItems();
    })
    $(".count-action input").focus(function () {
        this.select();
    });

    var curinutfocus;
    function searchProduct() {
        var val = $("#topsearchtitle").val();
        val = val.replace(/[^\u4e00-\u9fa5_a-zA-Z0-9]+/g,"");
        if (val == $('#topsearchtitle').attr("placeholder").replace(/[^\u4e00-\u9fa5_0-9]+/g,"")) val = "";
        if (val) {
            var url = ctx + "/product/list?querylike=" + encodeURI(encodeURI(val));
            if (window.location.href.indexOf("/product/list") != -1)
                window.location.href = url
            else window.open(url);
        }
    }
    function scroll_news() {
        $(function () {
            $('#toptxtfader li').eq(0).fadeOut('slow', function () {
                $(this).clone().appendTo($(this).parent()).fadeIn('slow');
                $(this).remove();
            });
        });
    }
    //setInterval('scroll_news()',3000);
    $(function () {
        jQuery('#topsearchtitle').focus(function () {
            var input = jQuery(this);
            if (input.val() == input.attr('placeholder')) {
                input.val('');
                input.removeClass('placeholder');
            }
            else input.css("color", "#333");
        }).blur(function () {
            var input = jQuery(this);
            if (input.val() == '' || input.val() == input.attr('placeholder')) {
                input.addClass('placeholder');
                input.val(input.attr('placeholder'));
                input.css("color", "#CECECE");
            }
        }).keyup(function () {
            var input = jQuery(this);
            if (input.val() == '' || input.val() == input.attr('placeholder')) {
                input.css("color", "#CECECE");
            } else input.css("color", "#333");
        });
        if (jQuery('#topsearchtitle').val() == '') jQuery('#topsearchtitle').val(jQuery('#topsearchtitle').attr('placeholder'));
        if (jQuery('#topsearchtitle').val() != jQuery('#topsearchtitle').attr('placeholder'))
            jQuery('#topsearchtitle').css("color", "#333");
        else jQuery('#topsearchtitle').css("color", "#CECECE");
    })

    function seetshop(id) {
        var shopname = $("#" + id).find("[name='shopname']").val();
        var shopurl = $("#" + id).find("[name='shopurl']").val();
        if (shopurl == "#") {
            var url = ctx + "/product/list?querylike=" + encodeURI(encodeURI(shopname))
            //window.location.href=url;
            window.open(url);
        } else if (shopurl == "") {
            var urlto = ctx + "/product/list?querylike=" + encodeURI(encodeURI(shopname))
            //window.location.href=urlto;
            window.open(urlto);
        } else {
            //window.location.href=shopurl;
            window.open(shopurl);
        }
    }

    window.onkeyup = function () {
        if (event.keyCode == 13) {
            searchProduct();
        }
    }
    document.getElementById('side').onmousewheel = function (event) {
        if (!event) event = window.event;
        this.scrollTop = this.scrollTop - (event.wheelDelta ? event.wheelDelta : -event.detail * 10);
        return false;
    }
    function head_splice(data) {
        var type = data.type;
        var data = data.data;
        switch (type) {
            case 'list':
                var product = data.products[0];
                $("#" + product.cartId).html(showHeadCommonProduct('', product));
                break;
            case 'complex':
                var products = data.products;
                for (var j = 0; j < products.length; j++) {
                    var product = products[j];
                    if (product.giftType == '') {
                        $("#" + product.cartId).html(showHeadComplexProduct('', product));
                    }
                }
                break;
            case 'purchase_gift':
                $("#" + data.promotionId).html(showHeadPurchaseGiftItem("", data));
                break;
            case 'money_off':
                $("#" + data.promotionId).html(showHeadMoneyOffItem("", data));
                break;
        }
    }
    var synBuyCountCache = {};
    var synBuyCountTimer = {};
    function head_synBuyCount(type, id, cid, pid, count, price, flag) {
        var buycount = count + '';
        var buycountCache = synBuyCountCache[pid + ''] + '';
        if (buycount == buycountCache) {
            return true;
        } else {
            synBuyCountCache[pid + ''] = buycount;
            var synBuyCountTimerPre = synBuyCountTimer[pid + ''];
            if (synBuyCountTimerPre > 0) {
                window.clearTimeout(synBuyCountTimerPre);
                synBuyCountTimer[pid + ''] = -1;
            }
            var synBuyCountTimerTemp = window.setTimeout(function () {
                synBuyCountTimer[pid + ''] = -1;
                head_synCartItems(type, id, cid, pid, count, price, flag);
            }, 600);
            synBuyCountTimer[pid + ''] = synBuyCountTimerTemp;
        }
    }

    var submit = false;
    function head_synCartItems(type, id, cid, pid, count, price, flag) {
        debugger;
        if (submit)return;
        submit = true;
        $w.httpRequest({
            url: "${ctx}/order/syncart",
            data: {
                "promotionType": type,
                "promotionId": id,
                "id": cid,
                "productid": pid,
                "count": count,
                "price": price,
                "check": flag
            },
            ok: function (ret) {
                var data = ret.Data;
                head_splice(data);
                $("#headCart_heji").text(data.price);
                //侧边栏同步
                if ($("#sideCart_heji")) {
                    splice(data);
                    $("#sideCart_heji").text(data.price);
                }
            },
            all: function () {
                submit = false;
            }
        })
    }
    function delCart(cid) {
        if (confirm("确定移除？")) {
            var ids = [];
            ids.push(cid);
            $w.httpRequest({
                url: apiroot + "/order/delCart",
                data: {"ids": ids},
                ok: function () {
                    window.location.reload();
                }
            })
        }
    }
    function head_inputAmount(type, id, cid, pid, obj) {
        $(obj).val($(obj).val().replace(/[^0-9]/g, ''));
        if ($(obj).val() == '0' || $(obj).val() == '') $(obj).val('1');
        if ($(obj).attr("maxnum") && $(obj).val()) {
            var max = parseInt($(obj).attr("maxnum"));
            max = max > 0 ? max : 0;
            if (parseInt($(obj).val()) > max) $(obj).val(max);
        }
        var price = $("#head_price" + cid).text();
        if (id) {
            price = $("#head_price" + id).text();
        }
        head_synBuyCount(type, id, cid, pid, $(obj).val(), price, true);
    }
    function head_addamount(type, id, cid, pid, obj) {
        var txt = $("[name='buynumInp']", $(obj).parent());
        var n = parseInt(txt.val());
        n++;
        txt.val(n);
        head_inputAmount(type, id, cid, pid, txt);
    }
    function head_lessenamount(type, id, cid, pid, obj) {
        var txt = $("[name='buynumInp']", $(obj).parent());
        var n = parseInt(txt.val());
        n--;
        txt.val(n);
        head_inputAmount(type, id, cid, pid, txt);
    }
    function showHeadBuySendProduct(h, product) {
        h += '<a class="item-img" target="_blank" href="' + ctx + "/product/info?priceid=" + product.id + '"><img src="' + getRandomFileserver() + product.photo + '" alt="" onerror="this.src=' + "'/website/images/noimg.jpg'" + '"></a>'
            + '<div class="item-cont">'
            + '<p class="name-specification">' + product.name + '&nbsp' + product.b_guige + '</p>'
            + '<p class="company">' + product.b_chanjia + '</p>'
            + ' <div class="price-count clear">'
            + '<em>￥' + product.totalPrice + '</em>'
            + '<a href="javascript:delCart(' + product.cartId + ');" class="delete-btn"></a>'
            + '<span class="count-action">'
            + '<i class="sub" onclick="head_lessenamount(' + "'complex',''," + product.cartId + ', ' + product.id + ', this);">-</i>'
            + '<input name="buynumInp" type="text" onkeyup="head_inputAmount(' + "'complex',''," + product.cartId + ', ' + product.id + ', this);" value="' + product.buyNum + '" maxnum="' + product.amount + '">'
            + '<i class="add" onclick="head_addamount(' + "'complex',''," + product.cartId + ', ' + product.id + ', this);">+</i>'
            + '</span>'
            + '<span class="count">' + product.buyNum + '</span>'
            + '</div>'
            + '</div>';
        if (product.buySend) {
            if (product.available) {
                h += '<p class="ms-active-msg"><a target="_blank" href="' + ctx + "/product/info?priceid=" + product.id + '">买送：' + product.name + '&nbsp' + product.b_guige + "×" + product.giftNum + '</a></p>';
            } else {
                h += '<p class="ms-active-msg"><a target="_blank" href="' + ctx + "/product/info?priceid=" + product.id + '">买' + product.standardNum + "送" + product.sendNum + '</a></p>';
            }
        }
        return h;
    }
    function showHeadActiveProduct(h, type, product, promotionId) {
        h += '<a class="item-img" target="_blank" href="' + ctx + "/product/info?priceid=" + product.id + '"><img src="' + getRandomFileserver() + product.photo + '" alt="" onerror="this.src=' + "'/website/images/noimg.jpg'" + '"></a>'
            + '<div class="item-cont">'
            + '<p class="name-specification">' + product.name + '&nbsp' + product.b_guige + '</p>'
            + '<p class="company">' + product.b_chanjia + '</p>'
            + ' <div class="price-count clear">'
            + '<em>￥' + product.totalPrice + '</i></em>'
            + '<a href="javascript:delCart(' + product.cartId + ');" class="delete-btn"></a>'
            + '<span class="count-action">'
            + '<i class="sub" onclick="head_lessenamount(\'' + type + '\',' + promotionId + ', ' + product.cartId + ', ' + product.id + ', this);">-</i>'
            + '<input name="buynumInp" type="text" onkeyup="head_inputAmount(\'' + type + '\',' + promotionId + ', ' + product.cartId + ', ' + product.id + ', this);" value="' + product.buyNum + '" maxnum="' + product.amount + '">'
            + '<i class="add" onclick="head_addamount(\'' + type + '\',' + promotionId + ', ' + product.cartId + ', ' + product.id + ', this);">+</i>'
            + '</span>'
            + '<span class="count">' + product.buyNum + '</span>'
            + '</div>'
            + '</div>';
        if (product.buySend) {
            if (product.available) {
                h += '<p class="ms-active-msg"><a target="_blank" href="' + ctx + "/product/info?priceid=" + product.id + '">买送：' + product.name + '&nbsp' + product.b_guige + "×" + product.giftNum + '</a></p>';
            } else {
                h += '<p class="ms-active-msg"><a target="_blank" href="' + ctx + "/product/info?priceid=" + product.id + '">买' + product.standardNum + "送" + product.sendNum + '</a></p>';
            }
        }
        return h;
    }
    function showHeadCommonProduct(h, product) {
        if (product) {
            h += '<a class="item-img" target="_blank" href="' + ctx + "/product/info?priceid=" + product.id + '"><img src="' + getRandomFileserver() + product.photo + '" alt="" onerror="this.src=' + "'/website/images/noimg.jpg'" + '"></a>'
                + '<div class="item-cont">'
                + '<p class="name-specification">' + product.name + '&nbsp' + product.b_guige + '</p>'
                + '<p class="company">' + product.b_chanjia + '</p>'
                + ' <div class="price-count clear">'
                + '<em>￥<i id="head_price' + product.cartId + '">' + product.totalPrice + '</i></em>'
                + '<a href="javascript:delCart(' + product.cartId + ');" class="delete-btn"></a>'
                + '<span class="count-action">'
                + '<i class="sub" onclick="head_lessenamount(' + "'list'" + ',null, ' + product.cartId + ', ' + product.id + ', this);">-</i>'
                + '<input name="buynumInp" type="text" onkeyup="head_inputAmount(' + "'list'" + ',null, ' + product.cartId + ', ' + product.id + ', this);" value="' + product.buyNum + '" maxnum="' + product.amount + '">'
                + '<i class="add" onclick="head_addamount(' + "'list'" + ',null, ' + product.cartId + ', ' + product.id + ', this);">+</i>'
                + '</span>'
                + '<span class="count">' + product.buyNum + '</span>'
                + '</div>'
                + '</div>';
        }
        return h;
    }
    function showHeadGiftProduct(h, product) {
        h += '<div class="cont-list-item clearfix">'
            + '<a class="item-img" target="_blank" href="' + ctx + "/product/info?priceid=" + product.id + '"><img src="' + getRandomFileserver() + product.photo + '" alt="" onerror="this.src=' + "'/website/images/noimg.jpg'" + '"></a>'
            + '<div class="item-cont">'
            + '<p class="name-specification"><em class="active-msg">赠品</em>' + product.name + '&nbsp' + product.b_guige + '</p>'
            + '<p class="company">' + product.b_chanjia + '</p>'
            + '<div class="price-count clear">'
            + '<em>￥0.00</em>'
            + '<span style="text-align:center; margin-right:65px;">' + product.buyNum + '</span>'
            + '</div>'
            + '</div>'
            + '</div>';
        return h;
    }
    function showHeadActiveItem(h, type, item) {
        var products = item.products;
        for (var j = 0; j < products.length; j++) {
            var product = products[j];
            if (product.gift) {
                h += showHeadGiftProduct("", product);
            } else if (product.giftType == '') {
                h += '<div class="cont-list-item cont-del-div clearfix">';
                h += showHeadActiveProduct("", type, product, item.promotionId);
                h += '</div>';
            }
        }
        return h;
    }
    function showHeadMoneyOffItem(h, item) {
        h += '<div class="active-title"><i id="head_price' + item.promotionId + '" style="display: none">' + item.totalPrice + '</i><span>满减</span>';
        if (item.available) {
            h += " 活动已购满" + item.standardMoney;
            if (item.discountRate > 0) {
                h += ',已打<em>' + item.discountRate + '</em>折';
            } else {
                h += ',已减<em>' + item.discountMoney + '</em>元';
            }
        } else {
            h += "活动";
            var rules = item.items;
            for (var i = 0; i < rules.length; i++) {
                var rule = rules[i];
                h += "满" + rule.offMoney;
                if (rule.discountRate > 0) {
                    h += '打' + rule.discountRate + '折,';
                } else {
                    h += '减' + rule.discountMoney + ',';
                }
            }
        }
        h += '</div>';
        h += showHeadActiveItem("", "money_off", item);
        return h;
    }
    function showHeadMoneyOffProducts(h, items) {
        if (items) {
            for (var i = 0; i < items.length; i++) {
                var item = items[i];
                h += '<div id="' + item.promotionId + '" class="cont-list-active cont-del-div">';
                h += showHeadMoneyOffItem("", item);
                h += '</div>';
            }
        }
        return h;
    }
    function showHeadComplexProduct(h, product) {
        h += '<i id="head_price' + product.cartId + '" style="display: none;">' + product.totalPrice + '</i>';
        h += showHeadBuySendProduct("", product);
        return h;
    }
    function showHeadComplexProducts(h, items) {
        if (items) {
            for (var i = 0; i < items.length; i++) {
                var product = items[i];
                if (product.giftType == '') {
                    h += '<div class="cont-list-active cont-del-div"><div id="' + product.cartId + '" class="cont-list-item cont-del-div clearfix">';
                    h += showHeadComplexProduct("", product);
                    h += '</div></div>';
                }
            }
        }
        return h;
    }
    function showHeadCommonProducts(h, items) {
        if (items) {
            for (var i = 0; i < items.length; i++) {
                var product = items[i];
                h += '<div id="' + product.cartId + '" class="cont-list-item cont-del-div clearfix">';
                h += showHeadCommonProduct("", product);
                h += '</div>';
            }
        }
        return h;
    }
    function showHeadPurchaseGiftItem(h, item) {
        h += '<div class="active-title">'
            + '<i id="head_price' + item.promotionId + '" style="display: none">' + item.totalPrice + '</i>'
            + '<span>满赠</span>';
        if (item.hasStandard) {
            if (item.standardMoney) {
                h += '活动已购满' + item.standardMoney + '元，已获得赠品<em>' + item.number + '</em>件';
            } else {
                h += '活动已购满' + item.standardAmount + '件，已获得赠品<em>' + item.number + '</em>件';
            }
        } else {
            if (item.standardMoney) {
                h += '活动购满' + item.standardMoney + '元，可获得赠品<em>' + item.number + '</em>件';
            } else {
                h += '活动购满' + item.standardAmount + '件，可获得赠品<em>' + item.number + '</em>件';
            }
        }
        h += '</div>';
        h += showHeadActiveItem("", "purchase_gift", item);
        return h;
    }
    function showHeadPurchaseGiftProducts(h, items) {
        if (items) {
            for (var i = 0; i < items.length; i++) {
                var item = items[i];
                h += '<div id="' + item.promotionId + '" class="cont-list-active cont-del-div">';
                h += showHeadPurchaseGiftItem("", item);
                h += '</div>';
            }
        }
        return h;
    }
    function showCartItems() {
        $.ajax({
            type: "get",
            url: ctx + "/order/cart?_=" + new Date().toTimeString(),
            dataType: "json",
            success: function (ret) {
                if (ret.MsgCode == '-1') {
                    $(".log-out").show();
                } else if (ret.MsgCode == '1') {
                    var data = ret.Data;
                    $("#cart_num").text(data.count);
                    $("#cart_num").removeClass("homecount");
                    if (data.count > 0) {
                        $("#cart_content_head").empty();
                        var itemHtml = "";
                        itemHtml += '<div class="cart-cont-list" id="side">';
                        itemHtml = showHeadCommonProducts(itemHtml, data.list);
                        itemHtml = showHeadComplexProducts(itemHtml, data.complex);
                        itemHtml = showHeadMoneyOffProducts(itemHtml, data.money_off);
                        itemHtml = showHeadPurchaseGiftProducts(itemHtml, data.purchase_gift);
                        itemHtml += '</div><div class="go-cart">'
                            + '<p></p><span>已选 <em>' + data.count + '</em> 件, 共<i id="headCart_heji">' + data.totalPrice + '</i>元</span>'
                            + '<a target="_blank" href="' + ctx + '/order/shopcart" class="go-cart-btn">去购物车结算</a>'
                            + '</div>';
                        $("#cart_content_head").html(itemHtml);
                        $("#cart_content_head").find(".cart-cont-list").mCustomScrollbar();
                    } else {
                        $(".cart-null").show();
                    }
                }
            }
        })
    }
</script>
